package com.live.dengdengtv.view;

import android.content.Context;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.live.dengdengtv.R;

/**
 * 控制密码是否显示
 * Created by wyf on 2016/8/26.
 */
public class PasswordView extends LinearLayout{

    private boolean isHidden = true;
    private  ImageButton imageButton;
    private EditText editText;

    public PasswordView(Context context, AttributeSet attrs) {
        super(context, attrs);

        setOrientation(LinearLayout.HORIZONTAL);
        setGravity(Gravity.CENTER_VERTICAL);

        editText = new EditText(context, attrs);
        LinearLayout.LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        lp.weight = 1;
        editText.setId(R.id.password_et);
        editText.setBackground(null);
        this.addView(editText, lp);

        initView(context);
    }
    private void initView(Context context) {
        isHidden = true;

        imageButton = new ImageButton(context);
        imageButton.setBackground(null);
        imageButton.setImageResource(R.mipmap.eyehalf_grey);
        imageButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                isHidden = !isHidden;
                if(isHidden){
                    //隐藏密码
                    editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    imageButton.setImageResource(R.mipmap.eyehalf_grey);
                }else {
                    //显示密码
                    editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    imageButton.setImageResource(R.mipmap.eye_grey);
                }
                editText.setSelection(editText.getText().length());//设置光标最后
            }
        });

        this.addView(imageButton);
    }



}
